package com.longrise.co.ui.carlife.fragment;

import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.longrise.co.R;
import com.longrise.co.api.ApiConstants;
import com.longrise.co.ui.carlife.adapter.NewsAdapter;
import com.longrise.co.ui.carlife.contract.CarLifeContract;
import com.longrise.co.ui.carlife.model.CarLifeModel;
import com.longrise.co.ui.carlife.model.NewsBean;
import com.longrise.co.ui.carlife.presenter.CarLifePresenter;
import com.longrise.co.utils.Constants;
import com.ly.common.base.BaseFragment;
import com.ly.common.baserx.RxBus;

import butterknife.Bind;
import butterknife.OnClick;

import static com.longrise.co.R.id.recyclerView;

/**
 * Created by luoyang on 2017-6-26.
 */

public class NewsMoreFragment extends BaseFragment<CarLifePresenter, CarLifeModel> implements CarLifeContract.View<NewsBean>, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnRecyclerViewItemClickListener {

    @Bind(R.id.head_title)
    TextView mHeadTitle;
    @Bind(recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.refresh_layout)
    SwipeRefreshLayout mRefreshLayout;
    @Bind(R.id.search_no_data_tv)
    TextView searchNoDataTv;
    @Bind(R.id.no_data_tip)
    RelativeLayout noDataTip;

    private NewsAdapter mAdapter;


    private int mPageCount = 0;
    private int mPageNum = 1;

    @Override
    protected int getLayoutResource() {
        return R.layout.common_recylerview_nodata;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    protected void initView() {
        mHeadTitle.setText("车主头条列表");
        searchNoDataTv.setText("未查询到车主头条相关信息");
        initRecyclerView();
        noDataTip.setVisibility(View.GONE);
        mPresenter.getLifeNewsList(mPageNum, ApiConstants.PAGE_SIZE_10, true);
    }

    private void initRecyclerView() {
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setColorSchemeColors(Constants.RECYCLER_RGB);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new NewsAdapter(null,_mActivity);

        mAdapter.setOnLoadMoreListener(this);
        mAdapter.openLoadMore(10, true);
        mAdapter.setOnRecyclerViewItemClickListener(this);
        mRecyclerView.setAdapter(mAdapter);
    }

    public static NewsMoreFragment newInstance() {
        return new NewsMoreFragment();
    }


    @Override
    public void onRefresh() {
        mPageNum = 1;
        noDataTip.setVisibility(View.GONE);
        mAdapter.notifyDataChangedAfterLoadMore(true);
        mPresenter.getLifeNewsList(mPageNum, ApiConstants.PAGE_SIZE_10, false);
    }


    @Override
    public void onLoadMoreRequested() {
        new Handler().postDelayed(new Runnable() {
            public void run() {
                if (mPageNum < mPageCount) {
                    //还有更多数据
                    mPresenter.getLifeNewsList(++mPageNum, ApiConstants.PAGE_SIZE_10, false);
                } else {
                    mAdapter.notifyDataChangedAfterLoadMore(false);
                }
            }

        }, 2000);
    }

    @Override
    public void onItemClick(View view, int i) {
        NewsBean.ListBean bean = mAdapter.getItem(i);
        if (bean != null) {
            RxBus.getInstance().post(Constants.RX_EVENT_NEXT_PAGE, NewDetailFragment.newInstance(bean));
        }
    }

    @Override
    public void requestSuccess(NewsBean info) {
        if (info != null && info.getList() != null && info.getList().size() > 0) {

            mPageCount = Integer.parseInt(info.getTotalnum());
            if (mPageNum == 1) {
                //刷新
                mAdapter.setNewData(info.getList());
            } else {
                //加载更多
                mAdapter.notifyDataChangedAfterLoadMore(info.getList(), true);
            }
        } else {
            //没有搜索到结果
            noDataTip.setVisibility(mPageNum == 1 ? View.VISIBLE : View.GONE);
            mPageNum--;
            mPageCount = mPageNum;
            mAdapter.notifyDataChangedAfterLoadMore(false);
            if (mPageNum > 1) {
                showLongToast("全部加载完毕");
            }
        }
    }

    @Override
    public void onComplete() {
        if (mRefreshLayout.isRefreshing()) {
            mRefreshLayout.setRefreshing(false);
        }
    }

    @OnClick(R.id.head_back)
    public void onViewClicked() {
        _mActivity.onBackPressed();
    }
}
